﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
     public class DAO_LoaiThanhTich
    {
         private DataServices data = new DataServices();
        public DataTable getAllThanhTich()
        {
            try
            {
                String sql = "SELECT * FROM " + DTO_LoaiThanhTich.TB_Name;
                return data.executeGetDataTable(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        public bool insertThanhTich(DTO_LoaiThanhTich thanhTich)
        {
            bool result = false;
            try
            {
                String sql = "INSERT INTO " + DTO_LoaiThanhTich.TB_Name
                                    + " ( " + DTO_LoaiThanhTich.TENTHANHTICH + " ) VALUES (@" + DTO_LoaiThanhTich.TENTHANHTICH + " ) ";
                SqlParameter p1 = new SqlParameter(DTO_LoaiThanhTich.TENTHANHTICH, thanhTich.TenLoaiThanhTich);
                result = data.executeUpdate(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return result;
        }

       
        public bool updateThanhTich(DTO_LoaiThanhTich thanhTich)
        {
            bool result = false;
            try
            {
                String sql = "UPDATE " + DTO_LoaiThanhTich.TB_Name
                                    + " SET " + DTO_LoaiThanhTich.TENTHANHTICH + " =@" + DTO_LoaiThanhTich.TENTHANHTICH
                                    + " WHERE " + DTO_LoaiThanhTich.MATHANHTICH + " =@" + DTO_LoaiThanhTich.MATHANHTICH;
                SqlParameter p1 = new SqlParameter(DTO_LoaiThanhTich.TENTHANHTICH, thanhTich.TenLoaiThanhTich);
                SqlParameter p2 = new SqlParameter(DTO_LoaiThanhTich.MATHANHTICH, thanhTich.MaLoaiThanhTich);
                result = data.executeUpdate(sql, p1, p2);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return result;
        }
        public bool deleteThanhTich(DTO_LoaiThanhTich thanhTich)
        {
            bool result = false;
            try
            {
                String sql = "deleteLoaiThanhTich";
                SqlParameter p1 = new SqlParameter(DTO_LoaiThanhTich.MATHANHTICH, thanhTich.MaLoaiThanhTich);
                result = data.updateFormProcedure(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return result;
        }

        public DataTable getThanhTichByName(string name)
        {
            try
            {
                String sql = "SELECT * FROM " + DTO_LoaiThanhTich.TB_Name + " WHERE "
                                    + DTO_LoaiThanhTich.TENTHANHTICH + " LIKE @" + DTO_LoaiThanhTich.TENTHANHTICH;
                SqlParameter p1 = new SqlParameter(DTO_LoaiThanhTich.TENTHANHTICH, "%" + name + "%");
                return data.executeGetDataTable(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }    
    }
}
